package com.realestate.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.AppConstant;
import com.realestate.constants.ComUnitName;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbProduct;
import com.realestate.model.BrtbProductAttributeValue;
import com.realestate.model.QBrtbAttribute;
import com.realestate.model.QBrtbCategory;
import com.realestate.model.QBrtbProduct;
import com.realestate.model.QBrtbProductAttributeValue;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ProductQueryDaoImpl extends GenericDAOImpl<BrtbProduct, Long> implements ProductQueryDao {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(ProductQueryDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@Override
	public List<BrtbProduct> getListproductByCategoryCode(String categoryCode, Long limit) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbCategory category = QBrtbCategory.brtbCategory;
		
		BooleanBuilder where = new BooleanBuilder();
		if(categoryCode != null && !categoryCode.equals(AppConstant.ALL)){
			where.and(category.categoryCode.eq(categoryCode));
		}
		where.and(category.recordStatus.eq(RecordStatus.O))
			 .and(product.recordStatus.eq(RecordStatus.O));
		
		query.from(product).leftJoin(product.brtbCategory, category).where(where).orderBy(product.makeDate.desc()).limit(limit);
		
		List<BrtbProduct> products = query.list(product);
		return products;
	}

	@Override
	public Page<BrtbProduct> getAllProduct(Pageable pageable) {
		JPQLQuery query = new JPAQuery(em());
		JPQLQuery queryCount = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbCategory category = QBrtbCategory.brtbCategory;
		
		BooleanBuilder where = new BooleanBuilder();
		where.and(category.recordStatus.eq(RecordStatus.O))
			 .and(product.recordStatus.eq(RecordStatus.O));
		
		query.from(product).leftJoin(product.brtbCategory, category).where(where).orderBy(product.makeDate.desc());
		query.offset(pageable.getOffset()).limit(pageable.getPageSize());
		
		List<BrtbProduct> products = query.list(product);
		long totalRows = queryCount.from(product).count();
		PageImpl<BrtbProduct> result = new PageImpl<BrtbProduct>(products, pageable, totalRows);
		return result;
	}

	@Override
	public String getProductAttrbuteValueByCode(Long productId,
			String attributeCode) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbAttribute attribute = QBrtbAttribute.brtbAttribute;
		QBrtbProductAttributeValue attributeValue = QBrtbProductAttributeValue.brtbProductAttributeValue;
		
		BooleanBuilder where = new BooleanBuilder();
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(attribute.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId))
			 .and(attribute.attributeCode.eq(attributeCode));
		
		query.from(attributeValue)
			.innerJoin(attributeValue.brtbProduct, product)
			.innerJoin(attributeValue.brtbAttribute, attribute)
			.where(where);
		
		BrtbProductAttributeValue attributeValueResult = query.uniqueResult(attributeValue);
		if(attributeValueResult != null){
			return attributeValueResult.getStrValue();
		}
		return "";
	}
}
